using System;
using System.Collections.Generic;
using System.Text;

using System.Drawing;

namespace Athena.Objects
{
    [Serializable]
    public class TextGlyph: IGlyph
    {
        public TextGlyph(TextGlyph oGlyph)
        {
            Text = oGlyph.Text;
            _oFillColorStart = oGlyph._oFillColorStart;
            _oFillColorEnd = oGlyph._oFillColorEnd;
            _Dimensions = oGlyph.Dimensions;
            _Location = oGlyph.Location;

            LabelBounds = oGlyph.LabelBounds;
            TextColor = oGlyph.TextColor;
            TextFont = oGlyph.TextFont;
        }

        public TextGlyph(PointF location) : this()
        {
            _Location = new PointF(location.X - 10, location.Y - 10);            
        }

        public TextGlyph()
        {
            _Dimensions = new System.Drawing.SizeF(30f, 40f);
            TextColor = Color.Black;            
            Text = "Text";            
        }

        public override string Type
        {
            get
            {
                return "text box";
            }
            set
            {
            }
        }

        public override bool HasPreview
        {
            get
            {
                return true;
            }
        }

        public override void Transform(PointF oDirection, ref Corner oCorner, bool Ctrl)
        {
            //RectangleF oldBounds = Bounds;

            base.Transform(oDirection, ref oCorner, Ctrl);

            RectangleF newBounds = Bounds;

            //float ratio = Math.Max(newBounds.Height/oldBounds.Height, newBounds.Width / oldBounds.Width);
            float size = Math.Min(newBounds.Height, newBounds.Width);

            if (size > 5)
            _oLabelFont = new Font(
                        _oLabelFont.FontFamily,
                        0.75f*size,
                        _oLabelFont.Style,
                        GraphicsUnit.Pixel,
                        _oLabelFont.GdiCharSet,
                        _oLabelFont.GdiVerticalFont);
        }

        public override void DrawLabel(Graphics g)
        {
            if (Dimensions.Height > 0)
            {


                RectangleF oLabelBounds = Bounds;
                

                Font oLabelFont = null;

                oLabelFont = _oLabelFont;
                
                /*
                oLabelFont = new Font(
                        _oLabelFont.FontFamily,
                        Math.Min(oLabelBounds.Height, oLabelBounds.Width),
                        _oLabelFont.Style,
                        GraphicsUnit.Pixel,
                        _oLabelFont.GdiCharSet,
                        _oLabelFont.GdiVerticalFont);
                                */

                SizeF oSize = g.MeasureString(Text, oLabelFont);

                oLabelBounds.X = oLabelBounds.X + (oLabelBounds.Width - oSize.Width) / 2f;
                oLabelBounds.Y = oLabelBounds.Y + (oLabelBounds.Height - oSize.Height) / 2f;

                g.DrawString(Text, oLabelFont, new SolidBrush(TextColor), oLabelBounds);
            }
        }

        public override System.Drawing.Bitmap GetPreview(int width, int height)
        {
            TextGlyph oGlyph = new TextGlyph(this);
            oGlyph.Location = new PointF();
            oGlyph.Dimensions = new SizeF(width, height);
            oGlyph.Text = "T";
            oGlyph.LabelBounds = Bounds;
            oGlyph.TextFont = new Font(FontFamily.GenericSansSerif, height);
            Bitmap oGraphic = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(oGraphic);
            oGlyph.DrawLabel(g);
            return oGraphic;            
        }

        public override void RenderGlyph(System.Drawing.Graphics g)
        {
            DrawLabel(g);
        }

        public override SBMLExtension.EmlRenderExtension.LocalStyle ToStyle(SBMLExtension.EmlRenderExtension.RenderInformation oInfo)
        {
            SBMLExtension.EmlRenderExtension.LocalStyle oResult = new SBMLExtension.EmlRenderExtension.LocalStyle();
            SBMLExtension.EmlRenderExtension.Group oGroup = new SBMLExtension.EmlRenderExtension.Group();
            oGroup.Stroke = oInfo.AddColorDefinition(_oEdgeColor);
            oGroup.StrokeWidth = _dEdgeThickness.ToString();
            if (_oFillColorStart == _oFillColorEnd)
                oGroup.Fill = oInfo.AddColorDefinition(_oFillColorStart);
            else
                oGroup.Fill = oInfo.AddLinearGradientDefinition(_oFillColorStart, _oFillColorEnd);

            AddLabelGlyph(oInfo, oGroup);

            oResult.Group = oGroup;
            return oResult;
        }

        public override object Clone()
        {
            return new TextGlyph(this);
        }
    }
}
